<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { get, post, put } from "@/net/index.js"; // 假设封装了 HTTP 请求的文件
import { ElMessage } from "element-plus";

const router = useRouter();


const sectionsData = ref([]);
const loading = ref(true);


// 加载板块数据
function loadData() {
  get("/api/forum/getBoards", (responseData) => {
    sectionsData.value = responseData;
    loading.value = false;
  }, (error) => {
    loading.value = false;
    ElMessage.warning(`数据加载失败: ${error.message}`);
  });
}

// 页面挂载时加载数据
onMounted(() => {
  loadData();
});

// 跳转到板块页面
function goToSection(id) {
  router.push(`/forums/${id}`);
}

// 控制展开/折叠的状态
const expandedSections = ref(new Set());

function toggleSection(id) {
  if (expandedSections.value.has(id)) {
    expandedSections.value.delete(id);
  } else {
    expandedSections.value.add(id);
  }
}


</script>

<template>
  <div class="home-page">
    <section class="welcome-banner">
      <h2>欢迎来到 Moonlit Alley 论坛！</h2>
      <p>在这里探索、建造、分享你的 Minecraft 创作，与社区成员交流！</p>
    </section>

    <section class="board-tree">
      <h3>板块管理</h3>

      <!-- 加载状态提示 -->
      <div v-if="loading" class="loading-indicator">
        <p>正在加载板块数据...</p>
        <el-loading :fullscreen="true"></el-loading> <!-- 使用 Element Plus 的加载组件 -->
      </div>

      <ul class="section-list" v-else>
        <li v-for="section in sectionsData" :key="section.id" class="section-item">
          <div @click="toggleSection(section.id)" class="section-header">
            <span>{{ section.name }}</span>
            <i :class="expandedSections.has(section.id) ? 'icon-expand' : 'icon-collapse'"></i>

          </div>
          <ul v-if="expandedSections.has(section.id)" class="sub-section-list">
            <li v-for="subSection in section.children" :key="subSection.id" @click.stop="goToSection(subSection.id)" class="sub-section-item">
              {{ subSection.name }}
              <hr>
            </li>
          </ul>
        </li>
      </ul>

    </section>
  </div>
</template>


<style scoped>
.sub-section-item hr {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.2); /* 半透明的分隔线 */
  margin: 5px 0; /* 间距 */
  transition: border-color 0.3s; /* 过渡效果 */
}

.sub-section-item:hover hr {
  border-color: rgba(255, 255, 255, 0.4); /* hover 时颜色变深 */
}

.action-buttons {
  display: flex;
  gap: 10px; /* 按钮间距 */
}

.edit-button,
.delete-button {
  padding: 8px 12px;
  border: none;
  border-radius: 4px;
  font-size: 0.9em;
  cursor: pointer;
  transition: background-color 0.3s, box-shadow 0.3s;
}

/* 编辑按钮样式 */
.edit-button {
  background-color: #409eff; /* 蓝色 */
  color: #fff;
}

.edit-button:hover {
  background-color: #66b1ff;
}

.edit-button:active {
  background-color: #337ab7;
  box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.2);
}

/* 删除按钮样式 */
.delete-button {
  background-color: #f56c6c; /* 红色 */
  color: #fff;
}

.delete-button:hover {
  background-color: #ff8787;
}

.delete-button:active {
  background-color: #dd4b4b;
  box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.2);
}
.add-section-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
  max-width: 100%;
  padding: 20px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background-color: #f9f9f9;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  margin-top: 20px;
}

.add-section-form h4 {
  margin: 0;
  font-size: 1.5em;
  color: #333;
}

.form-field {
  display: flex;
  flex-direction: column;
}

.form-field label {
  font-weight: bold;
  margin-bottom: 5px;
  color: #555;
}

.form-field input,
.form-field textarea {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 1em;
  resize: vertical;
}

.form-field input:focus,
.form-field textarea:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.2);
}

.add-button {
  padding: 10px 15px;
  background-color: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1em;
  cursor: pointer;
  transition: background-color 0.3s;
}

.add-button:hover {
  background-color: #66b1ff;
}

.add-button:active {
  background-color: #337ab7;
}
.home-page {
  padding: 20px;
  color: #fff;
  font-family: 'Minecraft', Arial, sans-serif;
  background: #333;
  min-height: 100vh;
  animation: fadeIn 1s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.welcome-banner {
  text-align: center;
  background: rgba(34, 34, 34, 0.85);
  padding: 30px;
  border-radius: 15px;
  margin-bottom: 40px;
  animation: slideDown 1.2s ease-in-out;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
}

@keyframes slideDown {
  from { transform: translateY(-20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.welcome-banner h2 {
  font-size: 36px;
  color: #e0e0e0;
}

.forum-sections {
  margin-bottom: 40px;
  opacity: 0;
  animation: fadeIn 1.5s ease-in-out forwards;
  animation-delay: 0.5s;
}

.forum-cards {
  display: flex;
  justify-content: space-between;
}

.forum-card {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
  transform: scale(1);
  animation: cardEnter 0.8s ease-in-out forwards;
  animation-delay: 0.5s;
}

.forum-card:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.6);
}

.forum-card:active {
  transform: scale(0.97);
}

@keyframes cardEnter {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.board-tree {
  background: #444;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  opacity: 0;
  animation: fadeInUp 1s ease-in-out forwards;
  animation-delay: 1s;
}

.section-list {
  list-style: none;
  padding: 0;
}

.section-item {
  margin-bottom: 15px;
}

.section-header {
  display: flex;
  align-items: center;
  cursor: pointer;
  background: #555;
  padding: 10px;
  border-radius: 5px;
  transition: background 0.3s;
}

.section-header:hover {
  background: #666;
}

.sub-section-list {
  list-style: none;
  padding: 10px 20px;
  background: #666;
  border-radius: 5px;
  margin-top: 10px;
}

.sub-section-item {
  padding: 5px 0;
  cursor: pointer;
  transition: color 0.2s;
}

.sub-section-item:hover {
  color: #ddd;
}

.icon-expand::before {
  content: '▼';
  margin-left: 8px;
}

.icon-collapse::before {
  content: '▶';
  margin-left: 8px;
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.board-tree-container {
  max-height: calc(100vh - 200px);
  overflow: auto;
}
</style>
